<?php
/**
 * Created by xiaosong
 * E-mail:4155433@gmail.com
 * Date: 2019/10/9
 * Time: 11:48
 */

namespace App\Models\user\observe;


use App\Models\shop\ChinaArea;
use App\Models\User;
use App\Models\user\Address;

class AddressObserve
{
    public function creating(Address $address)
    {
        $this->handler($address);
    }

    public function created(Address $address)
    {
    }
    public function updated(Address $address)
    {
    }
    public function deleted(Address $address)
    {
    }

    public function updating(Address $address)
    {
        $this->handler($address);
    }

    public function handler(Address $address)
    {
        if ($address->p_code){
            $p = ChinaArea::where('code',$address->p_code)->first();
            $address->p_id = $p->id;
            $address->p_name = $p->name;
        }

        if ($address->c_code){
            $c = ChinaArea::where('code',$address->c_code)->first();
            $address->c_id = $c->id;
            $address->c_name = $c->name;
        }

        if ($address->d_code){
            $d = ChinaArea::where('code',$address->d_code)->first();
            $address->d_id = $d->id;
            $address->d_name = $d->name;
        }else{
            $address->d_id = 0;
            $address->d_name = '';
        }

    }
}